package com.pda.common.pages.login;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.pda.common.R;
import com.pda.common.basemvp.activity.BaseActivity;
import com.pda.common.bean.LoginBean;
import com.pda.common.database.DataBaseControl;
import com.pda.common.pages.config.ConfigActivity;
import com.pda.common.pages.main.MainActivity;
import com.pda.common.tools.CheckVersion;
import com.pda.common.tools.SharedPreferencesManger;
import com.pda.common.utils.PDAUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity<LoginModel, LoginView, LoginPresent> implements LoginView {
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tvright)
    TextView tvright;
    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.ivu)
    ImageView ivu;
    @BindView(R.id.login_id)
    EditText loginId;
    @BindView(R.id.ivp)
    ImageView ivp;
    @BindView(R.id.login_p)
    EditText loginP;
    @BindView(R.id.cb_rp)
    CheckBox cbRp;
    @BindView(R.id.loginbt)
    Button loginbt;
    @BindView(R.id.configbt)
    Button configbt;
    @BindView(R.id.configcodebt)
    Button configcodebt;
    @BindView(R.id.tvr)
    TextView tvr;
    private static final int PERMISSION_REQUESTCODE = 100;

    private void requestPermission(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            List<String> permissionLists = new ArrayList<>();
            if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
                permissionLists.add(Manifest.permission.READ_EXTERNAL_STORAGE);
            }
            if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
                permissionLists.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }
            if(permissionLists.size()!=0){
                ActivityCompat.requestPermissions(this, permissionLists.toArray(new String[permissionLists.size()]), PERMISSION_REQUESTCODE);
            }
//            else
//            {
//
//            }
        }
    }

    @Override
    protected void initdata() {
        tvTitle.setText("登录");
        tvr.setText("版本号:V" + CheckVersion.getVersionName(this));
        if (SharedPreferencesManger.getRem()) {
            cbRp.setChecked(true);
            loginId.setText(SharedPreferencesManger.getUname());
            loginP.setText(SharedPreferencesManger.getUpass());
        } else {
            cbRp.setChecked(false);
            loginId.setText("");
            loginP.setText("");
        }
        requestPermission();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initdata();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public LoginModel createModel() {
        return new LoginModelImpl(this);
    }

    @Override
    public LoginView createView() {
        return this;
    }

    @Override
    public LoginPresent createPresenter() {
        return new LoginPresent();
    }

    @Override
    public void loginSuccess(final List<LoginBean.DataBean.ShowInfoBean> list) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                intentToActivityWithSParameter(LoginActivity.this, MainActivity.class, PDAUtil.keyUF, (Serializable) list);
                LoginActivity.this.finish();
            }
        });
    }

    @Override
    public void loginFail(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showMsg(str);
            }
        });
    }

    @OnClick({R.id.iv_title_left, R.id.cb_rp, R.id.loginbt, R.id.configbt, R.id.configcodebt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.cb_rp:
                SharedPreferencesManger.setRem(cbRp.isChecked());
                break;
            case R.id.loginbt:
                if (SharedPreferencesManger.getCid().length() == 0) {
                    showMsg("请配置企业编号");
                    return;
                }
                if (DataBaseControl.getProductAll().size() == 0) {
                    showMsg("请下载产品数据");
                    return;
                }
                if (DataBaseControl.getDealerAll().size() == 0) {
                    showMsg("请下载经销商数据");
                    return;
                }
                if (SharedPreferencesManger.getBoxMax() == 0) {
                    showMsg("请下载条码配置数据");
                    return;
                }
                String user = loginId.getText().toString();
                String pw = loginP.getText().toString();
                if (user.length() == 0) {
                    showMsg("请填写用户名");
                    return;
                }
                if (pw.length() == 0) {
                    showMsg("请填写密码");
                    return;
                }
                presenter.login(user, pw);
                break;
            case R.id.configbt:
                intentToActivityWithoutParameter(this, ConfigActivity.class);
                break;
        }
    }
}

